import {Router} from 'express';
import axios from 'axios';
const urllib = require("urllib-sync");
const querystring = require("querystring");
const backEndPath = require("../../server.config").backEndPath;
var router = Router()

router.post('/api/login', function (req, res) {
    //获取客户端基本信息
    /*let client_url = 'http://chaxun.1616.net/s.php?type=ip&output=json&callback=?&_='+Math.random();
    let Client = urllib.request(client_url, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        content: postData
    });
    let client_info = Client.data.toString();
    client_info = client_info.substr(2,client_info.length-3);
    client_info = JSON.parse(client_info.toString());*/
    let clientInfo ='';//querystring.stringify(client_info);

    let param = req.body.data;
    let postData = querystring.stringify(param);
    let re = urllib.request(`${backEndPath}/api/user/login`, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        content: postData+'&'+clientInfo
    });
    let data = JSON.parse(re.data.toString());
    +data.status_code === 200 ? req.session.authUser = data.data : void(0);
    console.log(data);
    return res.json(data)
});
//SSO单点登录用户认证
router.post('/api/ssoauth', function (req, res) {
    let param = req.body.data;
    let postData = querystring.stringify(param);
    let re = urllib.request(`${backEndPath}/api/user/ssoAuth`, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        content: postData
    });
    let data = JSON.parse(re.data.toString());
    +data.status_code === 200 ? req.session.authUser = data.data : void(0);
    console.log(data);
    return res.json(data)
});
//SSO单点登录用户信息绑定
router.post('/api/ssobind', function (req, res) {
    let param = req.body.data;
    let postData = querystring.stringify(param);
    let re = urllib.request(`${backEndPath}/api/user/ssoBind`, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        content: postData
    });
    let data = JSON.parse(re.data.toString());
    +data.status_code === 200 ? req.session.authUser = data.data : void(0);
    console.log(data);
    return res.json(data)
});
//发起POST /api/signUp 请求注销当前用户，并从 req.session 中移除
router.post('/api/signUp', function (req, res) {
    if(req.session.authUser){
        let param = {uid:req.session.authUser.uid,token:req.session.authUser.token};
        let postData = querystring.stringify(param);
        let re = urllib.request(`${backEndPath}/api/user/logout`, {
            method: 'POST',
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            content: postData
        });
    }
    req.session.destroy();
});
router.post('/api/personalSetting', function (req, res) {
    let param = req.body.data;
    let postData = querystring.stringify(param);
    let re = urllib.request(`${backEndPath}/api/user/personalSetting`, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        content: postData
    });
    let data = JSON.parse(re.data.toString());
    req.session.authUser = data.data;
    return res.json(data)
});
//更新用户头像缓存
router.post('/api/changeHeadImage', function(req, res) {
    let param = req.body.data;
    req.session.authUser.headImage = param.headImage;
    req.session.authUser.smallHeadImage = param.smallHeadImage;
    req.session.authUser.lastName = "Lin123";
    console.log("1.HeadImage = " + req.session.authUser.headImage);
    console.log("2.SmallHeadImage = " + req.session.authUser.smallHeadImage);
    console.log("3.lastName = " + req.session.authUser.lastName);
    req.session.save(function(err) {
        console.log(err);
    })
});
export default router
